package com.weijian.dal.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.weijian.dal.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

/**
 * 用户数据访问层
 *
 * @author weijian
 * @since 2024-12-20
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    /**
     * 根据邮箱查询用户
     *
     * @param email 邮箱地址
     * @return 用户实体
     */
    @Select("SELECT * FROM user WHERE email = #{email} AND deleted = 0")
    User selectByEmail(String email);

    /**
     * 根据用户名查询用户
     *
     * @param username 用户名
     * @return 用户实体
     */
    @Select("SELECT * FROM user WHERE username = #{username} AND deleted = 0")
    User selectByUsername(String username);

    /**
     * 检查邮箱是否已存在
     *
     * @param email 邮箱地址
     * @return 存在返回true
     */
    @Select("SELECT COUNT(*) > 0 FROM user WHERE email = #{email} AND deleted = 0")
    boolean existsByEmail(String email);

    /**
     * 检查手机号是否已存在
     *
     * @param phone 手机号
     * @return 存在返回true
     */
    @Select("SELECT COUNT(*) > 0 FROM user WHERE phone = #{phone} AND deleted = 0")
    boolean existsByPhone(String phone);

    /**
     * 检查用户名是否已存在
     *
     * @param username 用户名
     * @return 存在返回true
     */
    @Select("SELECT COUNT(*) > 0 FROM user WHERE username = #{username} AND deleted = 0")
    boolean existsByUsername(String username);
}